import { formatTime, pageConfig } from '../../../../utils/jk/jk-tool';
import { spinReportSpindlesReportGetSpindlesData } from '../../../../api/production/xsReport';

export default {
	data() {
		return {
			totalCount: 1,
			dateTimeRange: [],
			tableLoading: false,
			bCodeList: [
				{ id: 0, name: '全部' },
				{ id: 1, name: '正常断头' },
				{ id: 2, name: '开车断头' },
				{ id: 12, name: '落纱断头' }
			],
			statusList: [
				{ id: 0, name: '全部' },
				{ id: 1, name: '弱捻' },
				{ id: 4, name: '停锭' },
				{ id: 5, name: '断头' }
			],
			timeTypeList: [
				{ id: 1, name: '当班' },
				{ id: 2, name: '上一班次' },
				{ id: 3, name: '当天' },
				{ id: 4, name: '昨天' },
				{ id: 5, name: '当月' },
				{ id: 6, name: '一段时间' }
			],
			searchParams: {
				pageNo: pageConfig.pageNo,
				pageSize: pageConfig.pageSize,
				dateValue: 0,
				selectType: 1, // 1表示按班次; 2表示按机台
				bCode: '1',
				machineId: null,
				startDateTime: '',
				endDateTime: '',
				ifTest: this.ifTest,
				selectTime: 1
			},
			selectTypeList: [
				{ id: 1, name: '班次' },
				{ id: 2, name: '机台' }
			]
		};
	},
	methods: {
		onModifySearchParamChange() {
			this.searchParams.pageNo = 1;
			this.getDataRequest();
		},
		onPageChange(e) {
			this.searchParams.pageNo = e;
			this.getDataRequest();
		},
		onSelectTypeChange() {},
		onSelectTimeTypeChange() {
			const cpData = JSON.parse(JSON.stringify(this.searchParams));
			this.dateTimeRange = [cpData.startDateTime, cpData.endDateTime];
			this.getDtListRequest();
		},
		transferDateTime(x) {
			return formatTime(parseInt(x.substr(6, 13)));
		},
		transferData(arr, id) {
			const item = arr.find(x => x.id === id) || {};
			return item.name;
		},
		// 获取断头报工列表页数据
		getDtListRequest() {
			if (!this.dataItem.MachineName) return;
			this.refreshBtnLoading = true;
			const params = {
				selectType: this.searchParams.selectType,
				machineId: this.searchParams.IDMachine,
				ifTest: this.searchParams.ifTest,
				selectTime: this.searchParams.selectTime
			};
			if (this.searchParams.selectTime !== 6) {
				this.dateTimeRange = [];
			}
			params.timeBegin = this.dateTimeRange.length ? this.dateTimeRange[0] : '';
			params.timeEnd = this.dateTimeRange.length ? this.dateTimeRange[1] : '';
			return spinReportSpindlesReportGetSpindlesData(params).then(res => {
				this.refreshBtnLoading = false;
				if (res.data.status === 200) {
					if (res.data.res && res.data.res.length) {
						this.searchParams.startDateTime = this.transferDateTime(res.data.res[0].StartTime);
						this.searchParams.endDateTime = this.transferDateTime(res.data.res[0].EndTime);
						this.searchParams.pageNo = 1;
						this.getData();
					} else {
						this.searchParams.startDateTime = '';
						this.searchParams.endDateTime = '';
					}
				}
			});
		}
	}
};
